
package com.idealighter.game.gm.manager;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;

import com.idealighter.game.backpack.manager.BackpackMgr;
import com.idealighter.game.chat.manager.ChatMsgMgr;
import com.idealighter.game.core.constant.notice.Notice;
import com.idealighter.game.core.constant.notice.NoticeType;
import com.idealighter.game.dblog.service.DbLogService;
import com.idealighter.game.log.core.LogReason;
import com.idealighter.game.log.struct.gm.GmCmdLog;
import com.idealighter.game.notice.manager.NoticeMsgMgr;
import com.idealighter.game.player.struct.Player;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 前端gm命令管理脚本，带 . Script是可以热更新的,最好不要有状态
 * 
 * @date 2015年8月25日 上午11:12:12
 *
 */
@Singleton
public class GmCmdMgrScript {

  private static final Logger LOG = LoggerFactory.getLogger("GMLog");

  @Inject
  private BackpackMgr backpackMgr;
  @Inject
  private ChatMsgMgr chatMsgMgr;
  @Inject
  private NoticeMsgMgr noticeMsgMgr;
  @Inject
  @Named("closeGm") // 关闭gm.
  private boolean closeGm;

  /**
   * 执行gm命令 .
   * 
   * @param player 玩家.
   * @param cmd .
   */
  public void execute(Player player, String cmd) {
    if (closeGm) {
      LOG.error("服务器当前关闭gm执行，玩家[{}][{}]gm命令[{}]不能执行", player.getPlayerName(), player.getId(), cmd);
      return;
    }

    if (cmd == null || cmd.trim().equals("")) {
      LOG.error("gm指令不能为空");
      return;
    }

    // 请求参数
    String[] reqParams = cmd.split(" ");
    if (reqParams.length < 1) {
      LOG.error("请输入gm指令");
      return;
    }

    // gm指令名称，对应类的方法名
    String cmdName = reqParams[0];
    // gm指令参数
    String[] cmdArgs = new String[0];
    if (reqParams.length > 1) {
      cmdArgs = Arrays.copyOfRange(reqParams, 1, reqParams.length);
    }

    // 方法类型
    MethodType methodType = MethodType.methodType(void.class, Player.class, String[].class);
    // 方法条用器
    MethodHandle handle;
    try {
      handle = MethodHandles.lookup().findVirtual(getClass(), cmdName, methodType);
      handle.invoke(this, player, cmdArgs);
      // 记录gm执行日志
      DbLogService.log(new GmCmdLog(player, cmd));
    } catch (Throwable e) {
      LOG.error("gm指令执行错误", e);
    }
  }

  /**
   * 增加物品 .
   * 
   * @param player 玩家.
   * @param params .
   */
  public void addItem(Player player, String... params) {
    int itemId = Integer.parseInt(params[0]);
    int addNum = Integer.parseInt(params[1]);
    backpackMgr.addItem(player, itemId, addNum, LogReason.GM_ADD_ITEM);
  }

  /**
   * 聊天框系统公告 .
   * 
   * @param player 玩家.
   * @param params .
   */
  public void chatSysNotice(Player player, String... params) {
    chatMsgMgr.sendSysNoticeMsg(params[0]);
  }

  /**
   * 喇叭 .
   * 
   * @param player 玩家.
   * @param params .
   */
  public void horn(Player player, String... params) {
    chatMsgMgr.sendHornMsg(player.getPlayerName(), params[0]);
  }

  /**
   * 跑马灯公告 .
   * 
   * @param player 玩家.
   * @param params .
   */
  public void marqueeNotice(Player player, String... params) {
    noticeMsgMgr.sendMarqueeNoticeMsg(params[0], NoticeType.GAME, Notice.DEFAULT_INTERVAL,
        Notice.DEFAULT_TIMES, Notice.DEFAULT_COLOR);
  }

}
